<template>
  <el-container class="main-container">
    <!-- Header头部 -->
    <el-header>
      <img src="../assets/images/logo.png" alt="" />
      <el-menu mode="horizontal" background-color="#23262e" text-color="#fff" active-text-color="#409eff">
        <el-submenu index="1">
          <template slot="title">
            <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
            <img class="avatar" src="..//assets/logo.png" alt="" v-else />
            个人中心</template
          >
          <el-menu-item index="2-1"> <i class="el-icon-notebook-1"></i>用户资料</el-menu-item>
          <el-menu-item index="2-2"> <i class="el-icon-refresh-left"></i>重置密码</el-menu-item>
          <el-menu-item index="2-3"> <i class="el-icon-picture"></i>更换头像</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="logout"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <!-- Aside侧边栏 -->
    <el-container>
      <el-aside width="200px">
        <div class="user-info">
          <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
          <img class="avatar" src="..//assets/logo.png" alt="" v-else />
          <span>欢迎您 {{ user.nickname || user.username }}</span>
        </div>
        <el-menu
          background-color="#23262e"
          text-color="#fff"
          active-text-color="#409eff"
          :default-active="$route.path"
          unique-opened
          router
          class="aside-menu"
        >
          <template v-for="item in menus">
            <el-menu-item :index="item.indexPath" v-if="item.children === null" :key="item.iedexPath">
              <i :class="item.icon"></i>{{ item.title }}</el-menu-item
            >

            <el-submenu :index="item.indexPath" v-else :key="item.iedexPath">
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item :index="subItem.indexPath" v-for="subItem in item.children" :key="subItem.indexPath"
                ><i :class="subItem.icon"></i>{{ subItem.title }}
              </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <el-main>
          <router-view></router-view>
        </el-main>
        <el-footer>Footer底部</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>
<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/menu.js'
export default {
  data() {
    return {
      menus: []
    }
  },
  async created() {
    this.$store.dispatch('user/getUser')
    // 调用getMenuAPI方法 马上获取菜单数据
    const { data: res } = await getMenuAPI()
    if (res.code === 0) {
      // console.log(res)
      this.menus = res.data
    }
  },
  computed: {
    ...mapState('user', ['user'])
  },
  methods: {
    logout() {
      // 询问是否要退出
      this.$confirm('确定要退出吗？', '提示')
        .then(() => {
          // 点击了确定
          this.$router.push('/login')
          this.$store.commit('user/updateToken', '')
          this.$store.commit('user/updateUser', {})
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="less">
.main-container {
  height: 100%;
}
.el-header,
.el-aside {
  background-color: #22262e;
}
.el-main {
  background-color: #f2f2f2;
}
.el-footer {
  background-color: #eee;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.aside-menu {
  width: 200px;
}
.avatar {
  width: 35px;
  height: 35px;
  background-color: #fff;
  border-radius: 50%;
  margin-right: 15px;
  // 让图片按照原始比例完整的显示出来
  object-fit: cover;
}
.user-info {
  height: 70px;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
}
</style>
